package 一些基础的数据结构;

public class ReverseList {

    /**
     * 翻转单链表
     * @param head
     * @return
     */
    public static Node reverseLinkedList(Node head){
        Node pre = null;
        Node next = null;
        while (head != null){
            next = head.next;   // 先保存后继
            head.next = pre;
            pre = head;
            head = next;
        }
        return pre;
    }

    /**
     * 翻转双链表
     * @param head
     * @return
     */
    public static DoubleNode reverseDoubleList(DoubleNode head){
        DoubleNode pre = null;
        DoubleNode next = null;
        while(head != null){
            next = head.next;
            head.next = pre;
            head.last = next;
            pre = head;
            head = next;
        }
        return pre;
    }
}
